﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer.BLServerReference;
using BusinessLayer;
using System.Web.Security;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Configuration;
namespace WebFrontEnd
{
    public partial class CreateEvent : PageBase
    {
       
        #region Page Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {                
                InitiatePage(); 
            }           
        }
        #endregion

        #region Private Methods
        private void InitiatePage()
        {
            foreach (ListItem item in YorKinHTMLHelper.GetListItemsFromEnum(typeof(EventType)))
            {
                EventTypeDropDownList.Items.Add(item);
            }
            foreach (Hall hall in BL.GetAllHalls())
            {
                if (hall.City != null)
                {
                    HallInput.Items.Add(new ListItem(hall.Name + " - " + hall.City.Name, hall.ID.ToString()));   
                }
                else
                {
                    HallInput.Items.Add(new ListItem(hall.Name, hall.ID.ToString()));   
                }
            }
        }

        private void SetUpEventType(EventType eventType)
        {
            switch (eventType)
            {
                case EventType.Wedding:
                    FemaleActorInput.Visible = MaleActorLabel.Visible
                        = MaleActorInput.Visible
                        = FemaleActorLabel.Visible
                        = NameInput.Visible
                        = NameLabel.Visible
                        = true;

                    FemaleActorLabel.Text = "Enter the bride's name";
                    MaleActorLabel.Text = "Enter the groom's name";
                    NameLabel.Text = "Enter a title for the wedding";
                    break;
                case EventType.BarMitzva:
                    MaleActorInput.Visible
                        = MaleActorInput.Visible
                        = NameInput.Visible
                        = NameLabel.Visible
                        = true;
                    FemaleActorLabel.Visible = FemaleActorInput.Visible = false;
                    MaleActorLabel.Text = "Enter the strapping young lad's name";
                    NameLabel.Text = "Enter a title for the Bar Mitzvah";
                    break;
                case EventType.BatMitzva:
                    FemaleActorLabel.Visible
                        = FemaleActorInput.Visible
                        = NameInput.Visible
                        = NameLabel.Visible
                        = true;
                    MaleActorLabel.Visible = MaleActorInput.Visible = false;
                    FemaleActorLabel.Text = "Enter the young lady's name";
                    NameLabel.Text = "Enter a title for the Bat Mitzvah";
                    break;
                case EventType.Brit:
                    FemaleActorInput.Visible = MaleActorLabel.Visible
                        = MaleActorInput.Visible
                        = FemaleActorLabel.Visible
                        = false;
                    NameInput.Visible
                    = NameLabel.Visible
                    = true;
                    NameLabel.Text = "Enter a title for the Brit";
                    break;
                case EventType.Engagement:
                    FemaleActorInput.Visible = MaleActorLabel.Visible
                       = MaleActorInput.Visible
                       = FemaleActorLabel.Visible
                       = NameInput.Visible
                       = NameLabel.Visible
                       = true;
                    FemaleActorLabel.Text = "Enter the bride's name";
                    MaleActorLabel.Text = "Enter the groom's name";
                    NameLabel.Text = "Enter a title for the engagement";
                    break;
                default:
                    break;
            }
        }

        void TurnOnError(string text)
        {
            createAlertText.InnerText = text;
            createerroralert.Style["display"] = "block";
        }
        #endregion

        #region Event Handlers
        protected void EventTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            EventType et = (EventType)Enum.Parse(typeof(EventType),EventTypeDropDownList.SelectedItem.Value);
            SetUpEventType(et);
        }
        
        protected void CreateButton_Click(object sender, EventArgs e)
        {
            User currentUser = BL.GetUserByEmail(Page.User.Identity.Name);
            BusinessLayer.BLServerReference.Event aEvent = new BusinessLayer.BLServerReference.Event();
            aEvent.UserID = currentUser.ID;
            string format = "dd/MM/yyyy HH:mm";
            DateTime parseAttempt;
            if (!DateTime.TryParseExact(DateOfEventInput.Text + " " + TimeOfEventInput.Text,format,null,System.Globalization.DateTimeStyles.None,out parseAttempt))
            {
                TurnOnError("The date was not in the correct format.");
                return;
            }
            aEvent.DateOfEvent = parseAttempt;
            EventType t;
            if (!Enum.TryParse<EventType>(EventTypeDropDownList.SelectedValue,out t))
            {
                TurnOnError("The event type was invalid.");
                return;
            }
            aEvent.EventType = t;
            aEvent.HallID = int.Parse(HallInput.SelectedValue);
            aEvent.FemaleActor = FemaleActorInput.Text;
            aEvent.MaleActor = MaleActorInput.Text;
            aEvent.Name = NameInput.Text;
            var report = _bl.CreateEvent(aEvent);
            if (report.Valid)
            {
                Response.Redirect("Event.aspx?eventID=" + report.ModelObject.ID.ToString());                
            }
            else
            {
                TurnOnError(report.Message);
            }
        }
        #endregion

        #region Page Methods
        [WebMethod]
        public static string SendHallRequest(string hallName)
        {
            IBL bl = BusinessLayerFactory.GetCurrentBusinessLayer(ConfigurationManager.AppSettings.Get("DomainName"));
            User user = bl.GetUserByEmail(HttpContext.Current.User.Identity.Name);
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("admin@yems.somee.com", "admin@yems.somee.com");
            message.Body = @"There has been a request to add the hall """ + hallName + @""", you might wanna check it out.";
            message.Subject = @"Hall Request";
            try
            {
                bl.SendMail(message);
                return "SUCCESS";
            }
            catch (Exception)
            {
                return "FAILURE";
            }
        }
        #endregion
    }
}